package com.example.innovate.service.impl;

import com.example.innovate.domain.StudentInfoDO;
import com.example.innovate.domain.StudentInfoDOExample;
import com.example.innovate.mapper.StudentInfoDOMapper;
import com.example.innovate.service.StudentService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentInfoDOMapper studentInfoDOMapper;

    @Override
    public StudentInfoDO addStudent(StudentInfoDO studentInfoDO) {
        studentInfoDOMapper.insertSelective(studentInfoDO);
        return studentInfoDO;
    }

    @Override
    public void deleteStudent(int id) {
        studentInfoDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateStudent(StudentInfoDO studentInfoDO) {
        studentInfoDOMapper.updateByPrimaryKeySelective(studentInfoDO);
    }

    @Override
    public StudentInfoDO findStudentById(int id) {
        StudentInfoDO studentInfoDO = studentInfoDOMapper.selectByPrimaryKey(id);
        return studentInfoDO;
    }

    @Override
    public List<StudentInfoDO> findByUserId(int id,int pageNumber,int pageSize) {
        StudentInfoDOExample studentInfoDOExample = new StudentInfoDOExample();
        studentInfoDOExample.createCriteria()
                .andStuUserIdEqualTo(id);
        PageHelper.startPage(pageNumber,pageSize);
        List<StudentInfoDO> list = studentInfoDOMapper.selectByExample(studentInfoDOExample);
        return list;
    }

    @Override
    public List<StudentInfoDO> findAllStudent(int pageNumber,int pageSize) {
        StudentInfoDOExample studentInfoDOExample = new StudentInfoDOExample();
        PageHelper.startPage(pageNumber,pageSize);
        return studentInfoDOMapper.selectByExample(studentInfoDOExample);
    }

    @Override
    public List<StudentInfoDO> findAllNotPage() {
        StudentInfoDOExample studentInfoDOExample = new StudentInfoDOExample();
        return studentInfoDOMapper.selectByExample(studentInfoDOExample);
    }


    @Override
    public List<StudentInfoDO> findByClassName(String className, int pageNumber,int pageSize) {
        StudentInfoDOExample studentInfoDOExample = new StudentInfoDOExample();
        studentInfoDOExample.createCriteria()
                .andStuClassNameEqualTo(className);
        PageHelper.startPage(pageNumber,pageSize);
        return studentInfoDOMapper.selectByExample(studentInfoDOExample);
    }

    @Override
    public JSONArray valueList() {
        StudentInfoDOExample studentInfoDOExample = new StudentInfoDOExample();
        List<StudentInfoDO> list = studentInfoDOMapper.selectByExample(studentInfoDOExample);
        JSONArray array = new JSONArray();
        int i = 0;
        for(i=0;i<list.size();i++){
            array.add(list.get(i).getStuField());
        }
        return array;
    }
}
